<?php
session_start();
//require_once("../config/include.inc.php");
require_once("../config/include_tokyo.inc.php");
require_once("../class/Common_Function.php");
//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1 || $userRole == 2 || $userRole == 8){
	//管理员 快递员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}
/**
 * PHPExcel
 *
 * Copyright (C) 2006 - 2012 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 * @version    1.7.8, 2012-10-12
 */

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once '../class/PHPExcel.php';


// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

//时间 
$expressDate = $_GET["expressDate"];

// Set document properties
$objPHPExcel->getProperties()->setCreator("Vincent")
							 ->setLastModifiedBy("Vincent")
							 ->setTitle("Tracking Number List")		
							 ->setSubject("Tracking Number List")
							 ->setDescription("Tracking Number List")
							 ->setKeywords("Tracking Number List")
							 ->setCategory("Tracking Number List");


//----------标题--------------------------------------
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '时间')
            ->setCellValue('B1', '订单号码')
			->setCellValue('C1', '快递线路')
            ->setCellValue('D1', '快递单号');
         

//----------导出当月快递单-------------------------------

$commonFunction = new Common_Function();
$expressDate_sql = $expressDate."-01";
$sql = "SELECT * from orders o where YEAR('".$expressDate_sql."')=YEAR(deliver_time) AND MONTH('".$expressDate_sql."')=MONTH(deliver_time) and deliver_flag=2  order by create_time desc"; 

$result = $db->Execute($sql);
if(empty($result) || $result->RecordCount() < 1){
		$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A2', $expressDate)
            ->setCellValue('B2', '这个月没记录')
            ->setCellValue('C2', '')
			->setCellValue('D2', '');
}else{
	$i = 1;
	
	while (!$result->EOF) {
		$i = $i+1;
		$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue("A".$i, $expressDate)
            ->setCellValue("B".$i, $result->fields['order_no'])
            ->setCellValue("C".$i, $commonFunction->getExpressCompanyName($result->fields['express_company']))
			->setCellValue("D".$i, $result->fields['tracking_number']);
		$result->MoveNext();	
	}
	
}




// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($expressDate);


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="快递列表'.$expressDate.'"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
